package org.domain.projeto.converter;

import java.text.ParseException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.swing.text.MaskFormatter;

/**
 * Converter responsável por formatar um CPF sem máscara.
 * 
 * @author pedro.vasconcelos
 *
 */
public class CpfCnpjConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		String valor = (String) value;
		
		if(valor != null && valor.length() == 11){
			valor = valor.substring(0, 3) + "." + valor.substring(3,6) + "."
					+ valor.substring(6,9) + "-" + valor.substring(9,11);
			
		}else if(valor != null && valor.length() == 14){
			valor = valor.substring(0,2) + "." + valor.substring(2,5) + "."
					+valor.substring(5,8) + "/" + valor.substring(8,12) + "-"
					+valor.substring(12,14);
			
		}
		return valor;
	}

}
